# 练习题：炸⾦花棋牌游戏
# 需求：
# 1. 允许⽤户⼀次性输⼊多个玩家姓名，不限个数，然后为每个玩家随机⽣成3张牌
# 2. 你只有⼀付扑克牌，确保发出去的每张牌不重样
# 3. 牌需要有⿊桃、红桃、⽅⽚、梅花之分
import random


class PokerGame:
    def __init__(self, players):
        self.players = players
        self.deck = self.create_deck()
        self.deal_cards()

    def create_deck(self):
        suits = ['♠', '♥', '♣', '♦']
        ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
        deck = [(rank, suit) for suit in suits for rank in ranks]
        random.shuffle(deck)
        return deck

    def deal_cards(self):
        self.hands = {player: [self.deck.pop() for _ in range(3)] for player in self.players}
        print("发牌结果：")
        for player, hand in self.hands.items():
            print(f"{player}: {hand}")

    def rank_hands(self, hand):
        values = '--23456789TJQKA'
        counts = {'--': 0, '对子': 1, '顺子': 2, '金花': 3, '顺金': 4, '豹子': 5}

        hand_ranks = sorted(['--23456789TJQKA'.index(h[0]) for h in hand], reverse=True)
        if len(set(suit for rank, suit in hand)) == 1:
            if hand_ranks == list(range(hand_ranks[0], hand_ranks[0] - 3, -1)):
                return (counts['顺金'], hand_ranks)
            else:
                return (counts['金花'], hand_ranks)
        if hand_ranks == list(range(hand_ranks[0], hand_ranks[0] - 3, -1)):
            return (counts['顺子'], hand_ranks)
        if len(set(rank for rank, suit in hand)) == 2:
            return (counts['对子'], hand_ranks)
        if len(set(rank for rank, suit in hand)) == 1:
            return (counts['豹子'], hand_ranks)
        return (counts['--'], hand_ranks)

    def compare_hands(self, hand1, hand2):
        r1, r2 = self.rank_hands(hand1), self.rank_hands(hand2)
        if r1 > r2:
            return 1
        elif r1 < r2:
            return -1
        else:
            return 0

    def play(self):
        winner = max(self.players, key=lambda player: self.rank_hands(self.hands[player]))
        print(f"赢家是 {winner}，牌组是: {self.hands[winner]}")


if __name__ == "__main__":
    players = ["玩家1", "玩家2", "玩家3"]
    game = PokerGame(players)
    game.play()
